package me.passtime.projt.reg;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import me.passtime.projt.Main;
import me.passtime.projt.R;
import me.passtime.projt.util.PageJumpUtils;
import me.passtime.projt.util.UserHolder;

/**
 * Created by IntelliJ IDEA.
 * User: Lee
 * Date: 11-12-5
 * Time: 下午4:13
 */
public class RegisterStepTwoPage extends Activity implements View.OnClickListener {
    private static final String TAG = "PROJ-T";
    private EditText mVerificationCode;
    private String phoneNumber;
    private String password;
    private RegisterController mController;
    private ProgressDialog mDialog;
    private static final int DIALOG_PROGRESS = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reg_steptwo);
        mVerificationCode = (EditText) this.findViewById(R.id.reg2_verification_code);
        Bundle extras = getIntent().getExtras();
        phoneNumber = extras.getString("phoneNumber");
        password =extras.getString("password");
        mController=new RegisterController(this);

        this.findViewById(R.id.reg2_home_btn).setOnClickListener(this);
        this.findViewById(R.id.reg2_send_btn).setOnClickListener(this);
        this.findViewById(R.id.reg2_submit_btn).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.reg2_home_btn:
                PageJumpUtils.logoutAndJumpToMainPage(this);
                break;
            case R.id.reg2_send_btn:
                sendVerificationCodeAgain();
                break;
            case R.id.reg2_submit_btn:
                submitRegInfo();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        PageJumpUtils.logoutAndJumpToMainPage(this);
    }

    private void sendVerificationCodeAgain() {
         Log.d(TAG,mVerificationCode.getText().toString());
        showDialog(DIALOG_PROGRESS);
        mController.sendVerificationCode(phoneNumber,password);
    }

    private void submitRegInfo() {
        Log.d(TAG, UserHolder.phoneNumber);
        showDialog(DIALOG_PROGRESS);
        mController.submitRegisterInfo(phoneNumber,password,mVerificationCode.getText().toString().trim());
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG_PROGRESS:
                mDialog = new ProgressDialog(this);
                mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                mDialog.setMessage("正在处理，请等待......");
                return mDialog;
            default:
                return null;
        }
    }

    public void hideProgressDialog() {
        if (mDialog != null) {
            removeDialog(DIALOG_PROGRESS);
        }
    }
}
